#include <iostream>


using namespace std;

int pos [9][9];

bool solved = false;

void changeValue ()
{
    int r, c, v;

    cout << endl << "Row(0-8): ";
    cin >> r;
    cout << endl << "Column(0-8)";
    cin >> c;
    cout << "Enter Value(1-9): ";
    cin >> v;

    if(r > 8 || r < 0 || c < 0 || c > 8 || v < 1 || v > 9)
    {
        cout << endl << "Invalid Choice!";
    }else pos[r][c] = v;
}

int instructions()
{
    cout << "SuDroolKu 0.1 - By Drool Game Studios" << endl << endl;

    cout << "HALP!" << endl << endl;

    cin.clear();
    cout << "Press Enter / Return to continue...\n";
    cin.ignore(1,0);
}

bool input (string inp)
{
    if(inp == "quit" || inp == "q")
    {
        return false;
    }

    if(inp == "enter" || inp == "e")
    {
        changeValue();
        return true;
    }

    if(inp == "check" || inp == "c")
    {
        int row [9];
        int col [9];
        int blo [9];

        row[0] = pos[0][0] + pos[0][1] + pos[0][2] + pos[0][3] + pos[0][4] + pos[0][5] + pos[0][6] + pos[0][7] + pos[0][8];
        row[1] = pos[1][0] + pos[1][1] + pos[1][2] + pos[1][3] + pos[1][4] + pos[1][5] + pos[1][6] + pos[1][7] + pos[1][8];
        row[2] = pos[2][0] + pos[2][1] + pos[2][2] + pos[2][3] + pos[2][4] + pos[2][5] + pos[2][6] + pos[2][7] + pos[2][8];
        row[3] = pos[3][0] + pos[3][1] + pos[3][2] + pos[3][3] + pos[3][4] + pos[3][5] + pos[3][6] + pos[3][7] + pos[3][8];
        row[4] = pos[4][0] + pos[4][1] + pos[4][2] + pos[4][3] + pos[4][4] + pos[4][5] + pos[4][6] + pos[4][7] + pos[4][8];
        row[5] = pos[5][0] + pos[5][1] + pos[5][2] + pos[5][3] + pos[5][4] + pos[5][5] + pos[5][6] + pos[5][7] + pos[5][8];
        row[6] = pos[6][0] + pos[6][1] + pos[6][2] + pos[6][3] + pos[6][4] + pos[6][5] + pos[6][6] + pos[6][7] + pos[6][8];
        row[7] = pos[7][0] + pos[7][1] + pos[7][2] + pos[7][3] + pos[7][4] + pos[7][5] + pos[7][6] + pos[7][7] + pos[7][8];
        row[8] = pos[8][0] + pos[8][1] + pos[8][2] + pos[8][3] + pos[8][4] + pos[8][5] + pos[8][6] + pos[8][7] + pos[8][8];

        col[0] = pos[0][0] + pos[1][0] + pos[2][0] + pos[3][0] + pos[4][0] + pos[5][0] + pos[6][0] + pos[7][0] + pos[8][0];
        col[1] = pos[0][1] + pos[1][1] + pos[2][1] + pos[3][1] + pos[4][1] + pos[5][1] + pos[6][1] + pos[7][1] + pos[8][1];
        col[2] = pos[0][2] + pos[1][2] + pos[2][2] + pos[3][2] + pos[4][2] + pos[5][2] + pos[6][2] + pos[7][2] + pos[8][2];
        col[3] = pos[0][3] + pos[1][3] + pos[2][3] + pos[3][3] + pos[4][3] + pos[5][3] + pos[6][3] + pos[7][3] + pos[8][3];
        col[4] = pos[0][4] + pos[1][4] + pos[2][4] + pos[3][4] + pos[4][4] + pos[5][4] + pos[6][4] + pos[7][4] + pos[8][4];
        col[5] = pos[0][5] + pos[1][5] + pos[2][5] + pos[3][5] + pos[4][5] + pos[5][5] + pos[6][5] + pos[7][5] + pos[8][5];
        col[6] = pos[0][6] + pos[1][6] + pos[2][6] + pos[3][6] + pos[4][6] + pos[5][6] + pos[6][6] + pos[7][6] + pos[8][6];
        col[7] = pos[0][7] + pos[1][7] + pos[2][7] + pos[3][7] + pos[4][7] + pos[5][7] + pos[6][7] + pos[7][7] + pos[8][7];
        col[8] = pos[0][8] + pos[1][8] + pos[2][8] + pos[3][8] + pos[4][8] + pos[5][8] + pos[6][8] + pos[7][8] + pos[8][8];

        blo[0] = pos[0][0] + pos[0][1] + pos[0][2] + pos[1][0] + pos[1][1] + pos[1][2] + pos[2][0] + pos[2][1] + pos[2][2];
        blo[1] = pos[0][3] + pos[0][4] + pos[0][5] + pos[1][3] + pos[1][4] + pos[1][5] + pos[2][3] + pos[2][4] + pos[2][5];
        blo[2] = pos[0][6] + pos[0][7] + pos[0][8] + pos[1][6] + pos[1][7] + pos[1][8] + pos[2][6] + pos[2][7] + pos[2][8];
        blo[3] = pos[3][0] + pos[3][1] + pos[3][2] + pos[4][0] + pos[4][1] + pos[4][2] + pos[5][0] + pos[5][1] + pos[5][2];
        blo[4] = pos[3][3] + pos[3][4] + pos[3][5] + pos[4][3] + pos[4][4] + pos[4][5] + pos[5][3] + pos[5][4] + pos[5][5];
        blo[5] = pos[3][0] + pos[0][1] + pos[0][2] + pos[1][0] + pos[1][1] + pos[1][2] + pos[2][0] + pos[2][1] + pos[2][2];
        blo[6] = pos[6][0] + pos[6][1] + pos[6][2] + pos[7][0] + pos[7][1] + pos[7][2] + pos[8][0] + pos[8][1] + pos[8][2];
        blo[7] = pos[6][3] + pos[6][4] + pos[6][5] + pos[7][3] + pos[7][4] + pos[7][5] + pos[8][3] + pos[8][4] + pos[8][5];
        blo[8] = pos[6][6] + pos[6][7] + pos[6][8] + pos[7][6] + pos[7][7] + pos[7][8] + pos[8][6] + pos[8][7] + pos[8][8];

        if(row[0] == 45 && row[1] == 45 && row[2] == 45 && row[3] == 45 && row[4] == 45 && row[5] == 45 && row[6] == 45 && row[7] == 45 && row[8] == 45 &&
           col[0] == 45 && col[1] == 45 && col[2] == 45 && col[3] == 45 && col[4] == 45 && col[5] == 45 && col[6] == 45 && col[7] == 45 && col[8] == 45 &&
           blo[0] == 45 && blo[1] == 45 && blo[2] == 45 && blo[3] == 45 && blo[4] == 45 && blo[5] == 45 && blo[6] == 45 && blo[7] == 45 && blo[8] == 45)
        {
            solved = true;
            return false;
        }else cout <<endl << "Incorrect!" << endl;;

        return true;
    }

    if(inp == "help" || inp == "h")
    {
        bool q = false;
        do
        {
            instructions ();
        }while (q);

        return true;
    }else
        cout << endl << "Invalid Input!" << endl << "Type help for instructions" << endl;

    return true;
}

int main ()
{
    string cmd;

    do
    {
        cout << "SuDroolKu 0.2 - By Drool Game Studio" << endl << endl;

        cout << "     0  1  2   3  4  5   6  7  8" << endl;
        cout << "   +---------+---------+---------+" << endl;
        cout << " 0 | " << pos[0][0] << "  " << pos[0][1] << "  " << pos[0][2] << " | " << pos[0][3] << "  " << pos[0][4] << "  " << pos[0][5] << " | " << pos[0][6] << "  " << pos[0][7] << "  " << pos[0][8] << " |" << endl;
        cout << "   |         |         |         |" << endl;
        cout << " 1 | " << pos[1][0] << "  " << pos[1][1] << "  " << pos[1][2] << " | " << pos[1][3] << "  " << pos[1][4] << "  " << pos[1][5] << " | " << pos[1][6] << "  " << pos[1][7] << "  " << pos[1][8] << " |" << endl;
        cout << "   |         |         |         |" << endl;
        cout << " 2 | " << pos[2][0] << "  " << pos[2][1] << "  " << pos[2][2] << " | " << pos[2][3] << "  " << pos[2][4] << "  " << pos[2][5] << " | " << pos[2][6] << "  " << pos[2][7] << "  " << pos[2][8] << " |" << endl;
        cout << "   +---------+---------+---------+" << endl;
        cout << " 3 | " << pos[3][0] << "  " << pos[3][1] << "  " << pos[3][2] << " | " << pos[3][3] << "  " << pos[3][4] << "  " << pos[3][5] << " | " << pos[3][6] << "  " << pos[3][7] << "  " << pos[3][8] << " |" << endl;
        cout << "   |         |         |         |" << endl;
        cout << " 4 | " << pos[4][0] << "  " << pos[4][1] << "  " << pos[4][2] << " | " << pos[4][3] << "  " << pos[4][4] << "  " << pos[4][5] << " | " << pos[4][6] << "  " << pos[4][7] << "  " << pos[4][8] << " |" << endl;
        cout << "   |         |         |         |" << endl;
        cout << " 5 | " << pos[5][0] << "  " << pos[5][1] << "  " << pos[5][2] << " | " << pos[5][3] << "  " << pos[5][4] << "  " << pos[5][5] << " | " << pos[5][6] << "  " << pos[5][7] << "  " << pos[5][8] << " |" << endl;
        cout << "   +---------+---------+---------+" << endl;
        cout << " 6 | " << pos[6][0] << "  " << pos[6][1] << "  " << pos[6][2] << " | " << pos[6][3] << "  " << pos[6][4] << "  " << pos[6][5] << " | " << pos[6][6] << "  " << pos[6][7] << "  " << pos[6][8] << " |" << endl;
        cout << "   |         |         |         |" << endl;
        cout << " 7 | " << pos[7][0] << "  " << pos[7][1] << "  " << pos[7][2] << " | " << pos[7][3] << "  " << pos[7][4] << "  " << pos[7][5] << " | " << pos[7][6] << "  " << pos[7][7] << "  " << pos[7][8] << " |" << endl;
        cout << "   |         |         |         |" << endl;
        cout << " 8 | " << pos[8][0] << "  " << pos[8][1] << "  " << pos[8][2] << " | " << pos[8][3] << "  " << pos[8][4] << "  " << pos[8][5] << " | " << pos[8][6] << "  " << pos[8][7] << "  " << pos[8][8] << " |" << endl;
        cout << "   +---------+---------+---------+" << endl << endl;

        cout << "Show me your moves!: ";
        cin >> cmd;
    }while (input(cmd) );

    if(solved == true)
    {
        cout << endl <<"Sudoku Solved!";
    }

    cout << endl <<"Godbye!";

    return 0;
}
